﻿using AutoMapper;
using BasicData.Application.Input;
using BasicData.Application.Input.LineModel;
using BasicData.Application.Output;
using BasicData.Domain.Entities;
using BasicData.Infrastructure.Repositories;
using BasicData.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace BasicData.Application.Services.Line
{
    public class LineServices : ILineServices
    {
        IBasicModelRepository<LineModel> _repository;
        IBasicModelRepository<WorkshopModel> _workshopRepository;
        IMapper mapper;

        public LineServices(IBasicModelRepository<LineModel> repository, IBasicModelRepository<WorkshopModel> workshopRepository, IMapper mapper)
        {
            _repository = repository;
            _workshopRepository = workshopRepository;
            this.mapper = mapper;
        }

        public ApiResult<int> AddLine(LineInput input)
        {
            var line = mapper.Map<LineModel>(input);
            line.Id = YitIdHelper.NextId();
            line.CreatedAt = DateTime.Now;
            line.CreatedBy = "wyj";
            _repository.Create(line);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "添加成功",
                Data = 1
            };
        }

        public ApiResult<int> DeleteLine(long id)
        {
            ApiResult<int> res = new ApiResult<int>();
            var model = _repository.GetValue().FirstOrDefault(x => x.Id == id);
            if (model == null)
            {
                res.Code = ApiEnums.Fail;
                res.Message = "未找到该数据";
                return res;
            }
            model.IsDeleted = true;
            int i = _repository.Update(model);
            if (i > 0)
            {
                res.Code = ApiEnums.OK;
                res.Message = "删除成功";
                res.Data = i;
            }
            else
            {
                res.Code = ApiEnums.Error;
                res.Message = "删除失败";
                res.Data = i;
            }
            return res;
        }



        public ApiPaging<LineOutput> GetLine(PageInput input)
        {
            ApiPaging<LineOutput> res = new ApiPaging<LineOutput>();
            var line = _repository.GetValue().Where(x => x.IsDeleted == false);
            var workshop = _workshopRepository.GetValue().Where(x => x.IsDeleted == false);
            var query = from a in line
                        join b in workshop on a.WorkshopId equals b.Id
                        select new LineOutput()
                        {
                            Id = a.Id,
                            LineCode = a.LineCode,

                            LineName = a.LineName,
                            WorkshopId = a.WorkshopId,
                            WorkshopName = b.WorkshopName,
                            LineMsg = a.LineMsg,
                            CreatedBy = a.CreatedBy,
                            CreatedAt = a.CreatedAt,
                            UpdatedAt = a.UpdatedAt,
                        };
            var model = query.OrderByDescending(x => x.Id).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToList();

            return new ApiPaging<LineOutput>
            {
                TotalCount = query.Count(),
                PageDatas = model,
                Code = ApiEnums.OK,
                Message = "成功"
            }; 

        }

        public ApiResult<int> UpdateLine(UpdateLineInput input)
        {
            var modedl = mapper.Map<LineModel>(input);
            modedl.UpdatedAt = DateTime.Now;
            _repository.Update(modedl);
            return new ApiResult<int>()
            {
                Code = ApiEnums.OK,
                Message = "修改成功",

            };
        }
    }
}
